﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class UserPostController : Base
    {
        private readonly LoginUtil _LoginUtil;
        private readonly AdminUserPostService _AdminUserPostService;
        private readonly AdminUserGroupService _adminUserGroupService;
        private readonly LogService _logService;
        public UserPostController(LoginUtil LoginUtil, AdminUserPostService adminUserPostService, AdminUserGroupService adminUserGroupService, LogService logService)
        {
            _LoginUtil = LoginUtil;
            _AdminUserPostService = adminUserPostService;
            _adminUserGroupService = adminUserGroupService;
            _logService = logService;
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_Index)]
        public async Task<IActionResult> IndexAsync(int json = 0)
        {
            if (json == 1)
            {
                var pagedList = await _AdminUserPostService.GetAllAsync();
                return Json(new { code = 0, data = pagedList });
            }
            return View();
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
        public async Task<IActionResult> AddOrEditAsync(int id, int edit = 0, int pid = 0)
        {
            if (edit == 1)
            {
                var data = await _AdminUserPostService.GetInfo(id);
                return Json(new { status = 1, data });
            }
            ViewBag.id = id;
            ViewBag.pid = pid;
            return View();

        }


        [HttpPost]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
        [Log(Name = "新增岗位")]
        public async Task<IActionResult> CreatePostAsync(AdminUserPost cateModel)
        {
            if (ModelState.IsValid)
            {
                object insertPri = await _AdminUserPostService.InsertAsync(cateModel);
                if (insertPri != null)
                    return Json(new { status = 1, msg = "新增岗位 ID:" + insertPri.ToString() });
                else
                    return Json(new { status = 0, msg = "新增岗位 错误，请查看控制台日志！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost, ActionName("EditPost")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
        [Log(Name = "修改岗位")]
        public async Task<IActionResult> EditPostAsync(EditAdminUserPost editModel, int id, int dyEdit = 0)
        {
            
            if (ModelState.IsValid)
            {
                AdminUserPost adminUserDept = await _AdminUserPostService.GetInfo(id);
                if (dyEdit == 1)
                {
                    adminUserDept.Sort = editModel.Sort;

                    if (0 < await _AdminUserPostService.UpdateAsync(adminUserDept))
                        return Json(new { status = 1, msg = "修改岗位排序:" + adminUserDept.PostName + " Id： " + id });
                    else
                        return Json(new { status = 0, msg = "修改岗位排序失败:" + adminUserDept.PostName + " Id： " + id });

                }

                adminUserDept.PostName = editModel.PostName;
                adminUserDept.PostCode = editModel.PostCode;
                adminUserDept.Sort = editModel.Sort;
                adminUserDept.Remark = editModel.Remark;
                if (0 < await _AdminUserPostService.UpdateAsync(adminUserDept))
                    return Json(new { status = 1, msg = "修改岗位:" + adminUserDept.PostName + " Id： " + id });
                else
                    return Json(new { status = 0, msg = "修改岗位:" + adminUserDept.PostName + " Id： " + id });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost, ActionName("Delete")]
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_Delete)]
        [Log(Name = "删除岗位")]
        public async Task<IActionResult> DeleteAsync(int id)
        {
            if (await _AdminUserPostService.DeleteByPriKeyAsync(id) > 0)
                return Json(new { status = 1, msg = "删除岗位,Id： " + id });
            else
                return Json(new { status = 0, msg = "删除失败,Id： " + id });
        }

    }
}
